package com.datastructure.test.twoqueueofstack;

import java.util.LinkedList;
import java.util.Queue;

public class TwoQueueOfStack {

    public static void main(String[] args) {
        TwoQueueOfStack twoQueueOfStack = new TwoQueueOfStack();
        System.out.println(twoQueueOfStack.empty());
        twoQueueOfStack.push(1);
        System.out.println(twoQueueOfStack.top());
        System.out.println(twoQueueOfStack.empty());
    }

    Queue<Integer> queue1 = new LinkedList();
    Queue<Integer> queue2 = new LinkedList();

    public void push(int element) {
        queue1.add(element);
    }

    public int pop() {
        int size = queue1.size();
        for (int i = 0; i < size; i++) {
            if(i== (size -1)){
                Integer poll = queue1.poll();
                int length = queue2.size();
                for (int j = 0; j < length; j++) {
                    queue1.add(queue2.poll());
                }
                return poll==null?-10000:poll;
            } else {
                queue2.add(queue1.poll());
            }
        }
        return -10000;
    }

    public int top() {
        int size = queue1.size();
        for (int i = 0; i < size; i++) {
            Integer poll = queue1.poll();
            queue2.add(poll);
            if(i== size-1){
                int length = queue2.size();
                for (int j = 0; j < length; j++) {
                    queue1.add(queue2.poll());
                }
                return poll==null?-10000:poll;
            }
        }
        return -10000;
    }

    public boolean empty() {
        return queue1.isEmpty();
    }
}
